/**
 * @author harry
 * @date 2022/2/27/22:42
 */

package com.serviceOrchestration.controller;

import com.serviceOrchestration.annotation.SaveLog;
import com.serviceOrchestration.annotation.RoleRestriction;
import com.serviceOrchestration.pojo.Product;
import com.serviceOrchestration.pojo.ProductClient;
import com.serviceOrchestration.pojo.PurchaseRecord;
import com.serviceOrchestration.pojo.ReturnData;
import com.serviceOrchestration.atomic.AtomicUnitInfo;
import com.serviceOrchestration.service.ProductService;
import com.serviceOrchestration.util.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/product")
@RestController
public class ProductController {

    private final ProductService productService;

    @Autowired
    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    @RoleRestriction({Role.ADM, Role.SUP})
    @GetMapping("/allServices")
    public List<AtomicUnitInfo> allServices() {
        return productService.getAtomicUnitsInfo();
    }

    @SaveLog("查看所有产品")
    @RoleRestriction(Role.USR)
    @GetMapping("/allProducts")
    public List<ProductClient> allProducts() {
        return productService.getAllProducts();
    }

    @RoleRestriction({Role.ADM, Role.SUP})
    @GetMapping("/productList")
    public List<Product> productList() {
        return productService.getProductList();
    }

    @SaveLog("查看产品详情")
    @RoleRestriction({Role.USR, Role.ADM, Role.SUP})
    @GetMapping("/{productId}")
    public Product product(@PathVariable("productId") String productId) {
        return productService.getProduct(productId);
    }

//    @SaveLog("创建新产品")
    @RoleRestriction({Role.ADM, Role.SUP})
    @PutMapping("/create")
    @SuppressWarnings("unchecked")
    public ReturnData<?> create(@RequestBody Map<String, Object> params, @RequestAttribute("role") String role) {
        Map<String, Object> properties = (Map<String, Object>) params.get("properties");
        List<Map<String, Object>> services = (List<Map<String, Object>>) params.get("services");
        productService.createProduct(properties, services, role);
        return ReturnData.ok();
    }

//    @SaveLog("修改产品属性")
    @RoleRestriction({Role.ADM, Role.SUP})
    @PostMapping("/editProperty")
    @SuppressWarnings("unchecked")
    public void editProperty(@RequestBody Map<String, Object> params) {
        String productId = (String) params.get("id");
        Map<String, Object> modification = (Map<String, Object>) params.get("modification");
        productService.editProperties(productId, modification);
    }

//    @SaveLog("修改产品服务流程")
    @RoleRestriction({Role.ADM, Role.SUP})
    @PostMapping("/editService")
    @SuppressWarnings("unchecked")
    public void editService(@RequestBody Map<String, Object> params) {
        String productId = (String) params.get("id");
        List<Map<String, Object>> services = (List<Map<String, Object>>) params.get("services");
        productService.editServiceChain(productId, services);
    }

    @SaveLog("购买产品")
    @RoleRestriction(Role.USR)
    @PutMapping("/purchase")
    public Map<String, Object> purchase(
            @RequestBody Map<String, String> params,
            @RequestAttribute("sub") String userId
    ) {
        String productId = params.get("productId");
        String amount = params.get("amount");
        return productService.purchase(userId, productId, amount);
    }

    @SaveLog("查看购买记录")
    @RoleRestriction(Role.USR)
    @GetMapping("/purchaseHistory")
    public List<PurchaseRecord> purchaseHistory(@RequestAttribute("sub") String userId) {
        return productService.getPurchaseHistory(userId);
    }

    @RoleRestriction(Role.SUP)
    @PostMapping("/approve")
    public void approve(@RequestBody Map<String, Object> params) {
        String productId = (String) params.get("productId");
        productService.approveProduct(productId);
    }
}
